<?php
// @formatter:off
/**
 * @file Class.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-05 14:30:06 +0000 (Fri, 05 Jul 2013) $
 *
 * $Id: Class.php 89 2013-07-05 14:30:06Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/themes/default/Class.php $
 */
// @formatter:on

require_once dirname(__FILE__)."/config.php";

if(!isset($wcDefaults["class-WCThemeDefault"])) {
	$wcDefaults["class-WCThemeDefault"] = array();
}
if(!isset($wcDefaults["class-WCThemeDefault"]["templates"])) {
	$wcDefaults["class-WCThemeDefault"]["templates"] = false;
}

class WCThemeDefault extends WCTheme {
	public function loadAction($action) {
	}
	protected function load() {
		global $wcROOTDIR;
		global $wcROOTURI;
		global $wcThemeAssigns;
		global $wcVersions;
		global $wcAction;
		global $wcSubAction;
		global $wcProfile;
		global $wcGroup;
		global $wcCollection;
		global $wcComic;
		global $wcDirectory;
		global $wcTag;
		global $wcRule;
		global $wcGroupsHolder;
		global $wcUser;
		global $wcCanvasActions;
		global $wcDefaults;

		//
		// Setting theme basic information.
		$this->_name = "Default";
		$this->_themeRoot = dirname(__FILE__);
		$this->_themeUri = $wcROOTURI.substr($this->_themeRoot, strlen($wcROOTDIR));
		//
		// Setting theme template and parsers.
		$this->_parser = new WCParser();
		if($wcDefaults["class-WCThemeDefault"]["templates"]) {
			$this->_parser->templatesRoot($wcDefaults["class-WCThemeDefault"]["templates"]);
		} else {
			$this->_parser->templatesRoot("{$this->_themeRoot}/templates");
		}
		//
		// Checking if the group switching system should be activated.
		$groupsCount = 0;
		foreach($wcGroupsHolder->groups() as $group) {
			if($wcProfile->grantedGroup($group) && is_readable($group->root)) {
				$groupsCount++;
			}
		}
		if($groupsCount > 1) {
			$wcThemeAssigns["ENABLE-GROUPSWITCH"] = true;
		}
		//
		// Setting main template.
		if(defined("__SIMPLE_CONTAINER__")) {
			$this->_parser->load("/simplecontainer.html");
		} elseif(defined("__EMAIL__")) {
			$this->_parser->load("/email.html");
			require "{$this->_themeRoot}/controlers/email.php";
		} else {
			$this->_parser->load("/index.html");
			require "{$this->_themeRoot}/controlers/index.php";
		}

		$wcThemeAssigns["THEME-URI"] = WCSanitizer::WComixPath($this->_themeUri);
		$wcThemeAssigns["JQPLOT-URI"] = WCSanitizer::WComixPath("{$this->_themeUri}/includes/jqplot");

		switch($wcAction) {
			case WC_ACTION_RANDOM:
				require "{$this->_themeRoot}/controlers/contents/c.random.php";
				break;
			case WC_ACTION_COMIC_SHOW:
				require "{$this->_themeRoot}/controlers/contents/c.comic.php";
				break;
			case WC_ACTION_TAG_SHOW:
				require "{$this->_themeRoot}/controlers/contents/c.tag.php";
				break;
			case WC_ACTION_TAG_LIST:
				require "{$this->_themeRoot}/controlers/contents/c.tags.php";
				break;
			case WC_ACTION_DIRECTORY_SHOW:
				require "{$this->_themeRoot}/controlers/contents/c.directory.php";
				break;
			case WC_ACTION_DIRECTORY_LIST:
				require "{$this->_themeRoot}/controlers/contents/c.directories.php";
				break;
			case WC_ACTION_SEARCH:
				require "{$this->_themeRoot}/controlers/contents/c.search.php";
				break;
			case WC_ACTION_RULE_LIST:
				require "{$this->_themeRoot}/controlers/contents/c.rules.php";
				break;
			case WC_ACTION_RULE_SHOW:
				require "{$this->_themeRoot}/controlers/contents/c.rule.php";
				break;
			case WC_ACTION_COLLECTION_LIST:
				require "{$this->_themeRoot}/controlers/contents/c.collections.php";
				break;
			case WC_ACTION_COLLECTION_SHOW:
				require "{$this->_themeRoot}/controlers/contents/c.collection.php";
				break;
			case WC_ACTION_USER_LIST:
				require "{$this->_themeRoot}/controlers/contents/c.users.php";
				break;
			case WC_ACTION_USER_EDIT:
				require "{$this->_themeRoot}/controlers/contents/c.edituser.php";
				break;
			case WC_ACTION_USER_RESET:
				require "{$this->_themeRoot}/controlers/contents/c.resetuser.php";
				break;
			case WC_ACTION_USER_REGISTER:
				require "{$this->_themeRoot}/controlers/contents/c.registeruser.php";
				break;
			case WC_ACTION_STATS:
				require "{$this->_themeRoot}/controlers/contents/c.stats.php";
				break;
			case WC_ACTION_NOTHING:
			case WC_ACTION_LOGO:
				break;
			case WC_SACTION_COMIC_RENAME:
			case WC_SACTION_TAG_RENAME:
			case WC_SACTION_COLLECTION_RENAME:
			case WC_SACTION_RULE_RENAME:
			case WC_SACTION_DIRECTORY_RENAME:
				require "{$this->_themeRoot}/controlers/simplecontainer/s.editname.php";
				break;
			case WC_SACTION_COLLECTION_SET:
				require "{$this->_themeRoot}/controlers/simplecontainer/s.editcollection.php";
				break;
			case WC_SACTION_COMIC_TAGS:
				require "{$this->_themeRoot}/controlers/simplecontainer/s.edittags.php";
				break;
			case WC_SACTION_LOG_IN:
				require "{$this->_themeRoot}/controlers/simplecontainer/s.login.php";
				break;
			case WC_SACTION_GROUP_SWITCH:
				require "{$this->_themeRoot}/controlers/simplecontainer/s.switchgroup.php";
				break;
			case WC_PAGEERROR_403:
				require "{$this->_themeRoot}/controlers/contents/c.403.php";
				break;
			case WC_PAGEERROR_500:
				require "{$this->_themeRoot}/controlers/contents/c.500.php";
				break;
			case WC_ACTION_CREDITS:
				if(!$wcDefaults["no-credits"]) {
					if($wcProfile->allowedTo(WC_PERM_SPECIAL_CREDIT)) {
						$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.credits.html";
						$wcThemeAssigns["ACTIONEXTERNALSTYLE"] = WCSanitizer::WComixPath("{$this->_themeUri}/templates/contents/c.credits.css");
					} else {
						$wcThemeAssigns["error-string"] = "You're not allowed to credits page";
						require "{$this->_themeRoot}/controlers/contents/c.403.php";
					}
				} else {
					require "{$this->_themeRoot}/controlers/contents/c.404.php";
				}
				break;
			case WC_ACTION_VERSION:
				require "{$this->_themeRoot}/controlers/contents/c.version.php";
				break;
			case WC_ACTION_CANVAS:
				if(!$wcSubAction) {
					$wcThemeAssigns["error-string"] = "No subaction specified";
					require "{$this->_themeRoot}/controlers/contents/c.500.php";
				} elseif(isset($wcCanvasActions[$wcSubAction])) {
					if(isset($wcCanvasActions[$wcSubAction]["pre-js"]) && !is_readable($wcCanvasActions[$wcSubAction]["pre-js"])) {
						$wcThemeAssigns["error-message"] = "File '{$wcCanvasActions[$wcSubAction]["pre-js"]}' is specified but it doesn't exists";
						require "{$this->_themeRoot}/controlers/contents/c.500.php";
					} elseif(isset($wcCanvasActions[$wcSubAction]["post-js"]) && !is_readable($wcCanvasActions[$wcSubAction]["post-js"])) {
						$wcThemeAssigns["error-message"] = "File '{$wcCanvasActions[$wcSubAction]["post-js"]}' is specified but it doesn't exists";
						require "{$this->_themeRoot}/controlers/contents/c.500.php";
					} elseif(!is_readable($wcCanvasActions[$wcSubAction]["manager"]) || !is_readable($wcCanvasActions[$wcSubAction]["template"])) {
						if(!is_readable($wcCanvasActions[$wcSubAction]["manager"])) {
							$wcThemeAssigns["error-string"] = "File '{$wcCanvasActions[$wcSubAction]["manager"]}' doesn't exists";
						} elseif(!is_readable($wcCanvasActions[$wcSubAction]["template"])) {
							$wcThemeAssigns["error-string"] = "File '{$wcCanvasActions[$wcSubAction]["template"]}' doesn't exists";
						}
						require "{$this->_themeRoot}/controlers/contents/c.500.php";
					} else {
						require $wcCanvasActions[$wcSubAction]["manager"];
						$wcThemeAssigns["WC-CANVAS-TEMPLATE"] = $wcCanvasActions[$wcSubAction]["template"];
						$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.canvas.html";
						if(isset($wcCanvasActions[$wcSubAction]["pre-js"])) {
							$wcThemeAssigns["WC-CANVAS-PRE-JS"] = $wcCanvasActions[$wcSubAction]["pre-js"];
						}
						if(isset($wcCanvasActions[$wcSubAction]["post-js"])) {
							$wcThemeAssigns["WC-CANVAS-POST-JS"] = $wcCanvasActions[$wcSubAction]["post-js"];
						}
					}
				} else {
					require "{$this->_themeRoot}/controlers/contents/c.404.php";
				}
				break;
			case WC_EACTION_REGISTER:
				$wcThemeAssigns["ACTIONCONTENT"] = "/email/e.register.html";
				break;
			case WC_EACTION_USERNAME:
				$wcThemeAssigns["ACTIONCONTENT"] = "/email/e.username.html";
				break;
			case WC_EACTION_PASSWORD:
				$wcThemeAssigns["ACTIONCONTENT"] = "/email/e.password.html";
				break;
			case WC_PAGEERROR_404:
			default:
				require "{$this->_themeRoot}/controlers/contents/c.404.php";
				break;
		}
	}
}

$wcThemeClasses["default"] = "WCThemeDefault";
array_unshift($wcRightTools, array(
	"title" => "Search",
	"template" => dirname(__FILE__)."/templates/right-tools/searchbox.html"
));
?>
